package com.freedom.controller.w.core;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.freedom.bean.core.SysUser;
import com.freedom.bean.core.TestTable;
import com.freedom.service.core.SysUserService;
import com.freedom.sys.UserUtils;
import com.freedom.util.EasyUIDataGrid;
import com.freedom.vo.w.core.SysUserVo;
//import com.freedom.util.sys.RedisUtil;

@Controller
public class SysUserController extends BaseWebPaginationCRUDController<SysUserVo> {
	// 本地异常日志记录对象
	private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);
	
	@Autowired
	private SysUserService sysUserService;

	/**
	 * 跳转页面
	 * @return
	 */
	@RequiresPermissions("sys:user:view")
	@RequestMapping("wh001/wh011")
	public String init(){
		//return "test/test1"; //测试页面
		return "user/userlist";
	}
	
	/**
	 * 查询用户列表
	 * @param param
	 * @return
	 */
	@RequiresPermissions("sys:user:view")
	@RequestMapping(value="/wh001/wh012")
	@ResponseBody
	public EasyUIDataGrid userList(@RequestBody MultiValueMap<String, String> param){
		Map<String, Object> map = super.initParam(param);
		int rows = Integer.parseInt(param.getFirst("rows"));
		int page = Integer.parseInt(param.getFirst("page"));
		EasyUIDataGrid easyUIDataGrid = new EasyUIDataGrid();
		try {
			//SysUser user = UserUtils.getUser();
			/*if(RedisUtil.exists("userList"+user.getId()+page+"_"+rows)){ //判断是否有缓存
				easyUIDataGrid = (EasyUIDataGrid) RedisUtil.get("userList"+user.getId()+page+"_"+rows);
			}else{*/
				PageHelper.startPage(page, rows);
				List<SysUser> userList = sysUserService.getUserList(map);
				PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(userList);
				easyUIDataGrid.setTotal(pageInfo.getTotal());
				easyUIDataGrid.setRows(pageInfo.getList());
				//RedisUtil.set("userList"+user.getId()+page+"_"+rows, easyUIDataGrid);
			//}
		} catch (Exception e) {
			logger.error("错误信息：",e);
		}
		return easyUIDataGrid;
	}
	
	/**
	 * 添加用户信息
	 * @param file
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/wh001/03")
	@ResponseBody
	public SysUserVo saveSysUser(HttpServletRequest request, SysUserVo param) {
		SysUserVo vo = getVo();
		try {
			vo = sysUserService.saveSysUser(request,param);
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("添加用户：",e);
			vo.setErrCode("w-wh001-03");
		}
		return vo;
	}
	
	
	/*
	 * 
	 * ------以前测试数据------
	 */
	
	@RequiresPermissions("sys:user:view")
	@RequestMapping("wh001/testList1")
	public String testList1(){
		return "test/test1"; //测试页面
	}
	
	/**
	 * 测试请求数据
	 */
	@RequiresPermissions("sys:user:view")
	@RequestMapping("/wh001/testList1_1")
	@ResponseBody
	public EasyUIDataGrid testList1(@RequestParam int page,@RequestParam int rows){
		EasyUIDataGrid easyUIDataGrid = new EasyUIDataGrid();
		SysUser user = UserUtils.getUser();
		/*if(RedisUtil.exists("sysuserlist"+page+"-"+rows)){ //判断是否有缓存
			easyUIDataGrid = (EasyUIDataGrid) RedisUtil.get("sysuserlist"+user.getId()+page+"_"+rows);
		}else{*/
			PageHelper.startPage(page, rows);
			List<TestTable> userList = sysUserService.getTest1List(null);
			PageInfo<TestTable> pageInfo = new PageInfo<TestTable>(userList);
			easyUIDataGrid.setTotal(pageInfo.getTotal());
			easyUIDataGrid.setRows(pageInfo.getList());
			//RedisUtil.set("sysuserlist"+user.getId()+page+"_"+rows, easyUIDataGrid);
		//}
		System.out.println("userID："+user.getId());
		System.out.println("userLoginName："+user.getLoginName());
		logger.info("-----------------------------------ok-------------------------------------------------------------------------------------------");
		return easyUIDataGrid;
	}
	
	
}
